<template>
  <div class="chat-panel">
    <!-- 欢迎页(无消息时) -->
    <div v-if="messages.length === 0 && !loading" class="welcome-screen">
      <div class="welcome-content">
        <div class="welcome-icon"><img src="/root/dev_folder/boid_database/code/frontend/src/components/chat/logo.png" alt="arcCHAT" style="width: 102px; height: 102px;"/></div>
        <h1>欢迎使用 arcCHAT</h1>
        <p>我是您的生物信息学智能助手，可以帮您：</p>
        <div class="features">
          <a-card class="feature-card">
            <template #title>
              <search-outlined /> 搜索蛋白质
            </template>
            找大肠杆菌的Acr蛋白
          </a-card>
          <a-card class="feature-card">
            <template #title>
              <experiment-outlined /> BLAST比对
            </template>
            帮我比对这个序列...
          </a-card>
          <a-card class="feature-card">
            <template #title>
              <apartment-outlined /> 结构比对
            </template>
            比较蛋白质的3D结构
          </a-card>
          <a-card class="feature-card">
            <template #title>
              <book-outlined /> 知识问答
            </template>
            CRISPR-Cas9的作用机制
          </a-card>
        </div>
      </div>
    </div>

    <!-- 消息列表 -->
    <div v-else class="messages-container" ref="messagesContainer">
      <a-spin :spinning="loading" tip="加载历史消息...">
        <div v-for="(msg, index) in messages" :key="index" class="message-wrapper">
          <message-item :message="msg" />
        </div>
        
        <!-- 正在输入提示 -->
        <div v-if="sending" class="message-wrapper">
          <div class="message assistant-message">
            <div class="message-avatar">🤖</div>
            <div class="message-content">
              <a-spin size="small" />
              <span style="margin-left: 8px; color: #909399;">正在思考...</span>
            </div>
          </div>
        </div>
      </a-spin>
    </div>

    <!-- 输入区域 -->
    <div class="input-area">
      <div class="input-container">
        <a-textarea
          v-model:value="inputText"
          :rows="3"
          :maxlength="2000"
          :disabled="sending"
          placeholder="输入消息... (Shift+Enter换行, Enter发送)"
          @pressEnter="handleEnter"
        />
        <div class="input-actions">
          <span class="char-count">{{ inputText.length }}/2000</span>
          <a-button
            type="primary"
            :loading="sending"
            :disabled="!inputText.trim()"
            @click="handleSend"
          >
            <template #icon><send-outlined /></template>
            发送
          </a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, nextTick, watch } from 'vue'
import {
  SearchOutlined,
  ExperimentOutlined,
  ApartmentOutlined,
  BookOutlined,
  SendOutlined
} from '@ant-design/icons-vue'
import MessageItem from './MessageItem.vue'

const props = defineProps({
  messages: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  sending: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['send'])

const inputText = ref('')
const messagesContainer = ref(null)

// 发送消息
const handleSend = () => {
  if (!inputText.value.trim() || props.sending) return
  
  const messageToSend = inputText.value.trim()
  emit('send', messageToSend)
  // 发送后立即清空输入框
  inputText.value = ''
}

// Enter键处理
const handleEnter = (e) => {
  // Shift+Enter: 换行
  // Enter: 发送
  if (e.shiftKey) {
    return
  }
  
  e.preventDefault()
  handleSend()
}

// 滚动到底部
const scrollToBottom = () => {
  nextTick(() => {
    if (messagesContainer.value) {
      messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
    }
  })
}

// 监听消息变化,自动滚动
watch(() => props.messages.length, () => {
  scrollToBottom()
})

watch(() => props.sending, () => {
  scrollToBottom()
})
</script>

<style scoped>
.chat-panel {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 100px);
  background: #fff;
}

/* 欢迎页 */
.welcome-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
}

.welcome-content {
  text-align: center;
  max-width: 800px;
  width: 100%;
}

.welcome-icon {
  font-size: 72px;
  margin-bottom: 16px;
}

.welcome-content h1 {
  font-size: 28px;
  color: #2c3e50;
  margin-bottom: 10px;
  font-weight: 600;
}

.welcome-content > p {
  font-size: 15px;
  color: #606266;
  margin-bottom: 20px;
  line-height: 1.6;
}

.features {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-top: 30px;
}

.feature-card {
  text-align: left;
  cursor: pointer;
  transition: all 0.25s ease;
  border: 1px solid #e8e8e8;
}

.feature-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
  border-color: #d6e4ff;
}

:deep(.feature-card .ant-card-head) {
  padding: 10px 14px;
  min-height: auto;
}

:deep(.feature-card .ant-card-head-title) {
  font-size: 13px;
  font-weight: 600;
}

:deep(.feature-card .ant-card-body) {
  padding: 10px 14px;
  font-size: 12px;
  color: #909399;
  line-height: 1.5;
}

/* 消息容器 */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  background: #fafbfc;
}

.message-wrapper {
  margin-bottom: 24px;
}

.message {
  display: flex;
  gap: 12px;
  max-width: 80%;
}

.assistant-message {
  margin-right: auto;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  background: #f0f0f0;
}

.message-content {
  flex: 1;
  padding: 12px 16px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

/* 输入区域 */
.input-area {
  border-top: 1px solid #e8e8e8;
  padding: 20px 24px;
  background: #fff;
}

.input-container {
  max-width: 100%;
}

:deep(.ant-input) {
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  font-size: 14px;
}

:deep(.ant-input:focus) {
  border-color: #91caff;
  box-shadow: 0 0 0 2px rgba(91, 164, 255, 0.08);
}

.input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}

.char-count {
  font-size: 12px;
  color: #909399;
}

/* 滚动条 */
.messages-container::-webkit-scrollbar {
  width: 5px;
}

.messages-container::-webkit-scrollbar-thumb {
  background: #d9d9d9;
  border-radius: 3px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
  background: #bfbfbf;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .welcome-content {
    max-width: 100%;
    padding: 0 10px;
  }
  
  .welcome-icon {
    font-size: 60px;
  }
  
  .welcome-content h1 {
    font-size: 24px;
  }
  
  .welcome-content > p {
    font-size: 14px;
    margin-bottom: 16px;
  }
  
  .features {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .features {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>

